/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import entidades.Estado;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author fernandogomespinto
 */
public class EstadoDao extends Dao<Estado> {
    
     public EstadoDao() throws SQLException {
    }


    @Override
    public void save(Estado obj) throws SQLException {
        PreparedStatement stmt = getConnection().prepareStatement(
                "INSERT INTO "
                + "estado( nome, sigla ) "
                + "VALUES( ?, ? );" );
        stmt.setString( 1, obj.getNome() );
        stmt.setString( 2, obj.getSigla() );
        stmt.executeUpdate();
        stmt.close();

    }

    @Override
    public void update(Estado obj) throws SQLException {
        
      PreparedStatement stmt = getConnection().prepareStatement(
                "UPDATE estado "
                + "SET"
                + "    nome = ?,"
                + "    sigla = ? "
                + "WHERE"
                + "    id = ?;" );
        stmt.setString( 1, obj.getNome() );
        stmt.setString( 2, obj.getSigla() );
        stmt.setLong( 3, obj.getId() );
        stmt.executeUpdate();
        stmt.close();

    }

    @Override
    public void delete(Estado obj) throws SQLException {
         PreparedStatement stmt = getConnection().prepareStatement(
                "DELETE FROM estado "
                + "WHERE"
                + "    id = ?;" );
        stmt.setLong( 1, obj.getId() );
        stmt.executeUpdate();
        stmt.close();

    }

    @Override
    public List<Estado> listAll() throws SQLException {
       List<Estado> lista = new ArrayList<Estado>();
        PreparedStatement stmt = getConnection().prepareStatement(
                "SELECT * FROM estado;" );
        ResultSet rs = stmt.executeQuery();
        while ( rs.next() ) {
            Estado e = new Estado();
            e.setId( rs.getLong( "id" ) );
            e.setNome( rs.getString( "nome" ) );
            e.setSigla( rs.getString( "sigla" ) );
            lista.add( e );
        }
        rs.close();
        stmt.close();
        return lista;
    
    }
   
    
}
